package com.wgchao.patientcenter.domain.service.impl;

import com.wgchao.patientcenter.common.ResultCommon;
import com.wgchao.patientcenter.domain.entity.MdmpGrxx;
import com.wgchao.patientcenter.domain.repository.MdmpGrxxRepository;
import com.wgchao.patientcenter.domain.service.GrxxService;
import com.wgchao.patientcenter.mapstruct.dto.MdmpGrxxCreateDTO;
import com.wgchao.patientcenter.mapstruct.dto.MdmpGrxxDTO;
import com.wgchao.patientcenter.mapstruct.dto.MdmpGrxxUpdateDTO;
import com.wgchao.patientcenter.mapstruct.mapper.MdmpGrxxCreateMapper;
import com.wgchao.patientcenter.mapstruct.mapper.MdmpGrxxUpdateMapper;
import com.wgchao.patientcenter.mapstruct.mapper.expand.MdmpGrxxMapperExpand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Optional;

/**
 * @ClassName: GrxxServiceImpl
 * @author: wgchao
 * @createTime: 2019/11/3 1:34 PM
 * @Description: 患者基本信息业务
 */
@Service
@Transactional
public class GrxxServiceImpl implements GrxxService {
    @Autowired
    private MdmpGrxxRepository grxxRepository;
    @Autowired
    private MdmpGrxxMapperExpand grxxMapperExpand;
    @Autowired
    private MdmpGrxxCreateMapper grxxCreateMapper;
    @Autowired
    private MdmpGrxxUpdateMapper grxxUpdateMapper;


    /**
     *  新增患者信息
     * @param grxxCreateDTO 患者信息
     * @return
     * @throws Exception
     */
    @Override
    public ResultCommon<MdmpGrxxDTO> save(MdmpGrxxCreateDTO grxxCreateDTO) throws Exception {
        MdmpGrxx grxx = grxxCreateMapper.toEntity(grxxCreateDTO);
        grxx.setCreateTime(new Date());
        grxx.setDel(false);

        grxx = grxxRepository.save(grxx);

        grxxMapperExpand = grxxMapperExpand.setInstance(grxx);
        MdmpGrxxDTO grxxDTO = grxxMapperExpand.toDTO(grxx);
        grxxDTO = grxxMapperExpand.getExtInfos(grxxDTO);
        return ResultCommon.success().setData(grxxDTO);
    }

    /**
     * 查询用户基本信息
     * @param idcard 证件编号
     * @return
     * @throws Exception
     */
    @Override
    public ResultCommon<MdmpGrxxDTO> search(String idcard) throws Exception {
        if(StringUtils.isEmpty(idcard)){
            return ResultCommon.fail("证件号缺失");
        }
        Optional<MdmpGrxx> grxxOptional = grxxRepository.findById(idcard);
        if(grxxOptional.isPresent()){
            MdmpGrxx grxx = grxxOptional.get();
            grxxMapperExpand = grxxMapperExpand.setInstance(grxx);
            MdmpGrxxDTO grxxDTO = grxxMapperExpand.toDTO(grxx);
            grxxDTO = grxxMapperExpand.getExtInfos(grxxDTO);
            return ResultCommon.success().setData(grxxDTO);
        }
        return ResultCommon.success();
    }

    /**
     * 删除患者基本信息
     * @param idcard 证件编号
     * @return
     * @throws Exception
     */
    @Override
    public ResultCommon delete(String idcard) throws Exception {
        Optional<MdmpGrxx> grxxOptional = grxxRepository.findById(idcard);
        if(!grxxOptional.isPresent()){
            return ResultCommon.fail("患者信息不存在");
        }
        MdmpGrxx grxx = grxxOptional.get();
        grxx.setDel(true);
        grxx.setDelTime(new Date());

        grxx = grxxRepository.save(grxx);

        return ResultCommon.success();
    }

    /**
     * 更新患者信息
     * @param grxxUpdateDTO 患者信息
     * @return
     * @throws Exception
     */
    @Override
    public ResultCommon<MdmpGrxxDTO> update(MdmpGrxxUpdateDTO grxxUpdateDTO) throws Exception {
        Optional<MdmpGrxx> grxxOptional = grxxRepository.findById(grxxUpdateDTO.getIdcard());
        if(!grxxOptional.isPresent()){
            return ResultCommon.fail("患者信息不存在");
        }
        MdmpGrxx grxx = grxxOptional.get();
        if(grxx.isDel()){
            return ResultCommon.fail("患者已删除");
        }

        grxx = grxxUpdateMapper.toEntity(grxxUpdateDTO);
        grxx = grxxRepository.save(grxx);

        grxxMapperExpand = grxxMapperExpand.setInstance(grxx);
        MdmpGrxxDTO grxxDTO = grxxMapperExpand.toDTO(grxx);
        grxxDTO = grxxMapperExpand.getExtInfos(grxxDTO);
        return ResultCommon.success().setData(grxxDTO);
    }

}
